<?php

declare(strict_types=1);

namespace App\Application\Admin\Controller;

use App\Annotation\Api;
use App\Annotation\View;
use App\Application\Admin\Controller\RequestParam\VipSubmitRequestParam;
use App\Application\Admin\Middleware\AdminMiddleware;
use App\Application\Admin\Model\Room;
use App\Application\Admin\Service\RoomService;
use App\Application\Admin\Service\AdminUserService;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Application\Admin\Model\Vip;
use App\Application\Admin\Model\Store;
use Hyperf\HttpServer\Annotation\RequestMapping;

#[Middleware(AdminMiddleware::class)]
#[Controller("admin/vip")]
class VipController extends AbstractController
{
    #[View]
    #[GetMapping("index")]
    public function index()
    {

    }

    #[Api]
    #[GetMapping("index/lists")]
    function lists()
    {
        $where = [];

        $where[] = ['admin_id', '=', $this->getadminid()];
        $storelist = Store::where($where)
            ->orderBy('created_at', 'DESC')
            ->pluck('store_name', 'store_id');
        $vip_name = $this->request->input('name', '');

        if ($vip_name != '') {
            $where[] = ['name', 'like', '%' . $vip_name . '%'];
        }
        $lists = Vip::where($where)
            ->orderBy('vip_id', 'DESC')
            ->paginate()->toArray();
        foreach ($lists['data'] as $k => $v) {
            //店铺名称
            $lists['data'][$k]['store_name'] = $storelist[$v['store_id']];

        }
        return compact('lists', 'storelist');
    }

    #[Api]
    #[RequestMapping("edit", ["POST", "PUT"])]
    function submitEdit()
    {
        $request_param = new VipSubmitRequestParam();
        $request_param->validatedThrowMessage();
        $vip_id = $request_param->getVipId();
        $res = Vip::createVip($vip_id, [
            'name' => $request_param->getName()
            , 'price' => $request_param->getPrice()
            , 'give_price' => $request_param->getGivePrice()
            , 'end_time' => $request_param->getEndTime()
            , 'sort' => $request_param->getSort()
            , 'status' => $request_param->getStatus()
            , 'admin_id' => $this->getadminid()
            , 'store_id' => $request_param->getStoreId()
        ]);

        return $res ? $this->returnSuccessJson(compact('res', 'vip_id')) : $this->returnErrorJson();
    }

    #[View]
    #[GetMapping("edit")]
    function edit()
    {
        $vip_id = (int)$this->request->input('vip_id', 0);

        return ['title' => $vip_id > 0 ? '编辑优惠卷' : '新增优惠卷'];
    }


    #[Api]
    #[GetMapping("edit/{vip_id}")]
    function editInfo(int $vip_id)
    {
        $vip = Vip::where('vip_id', $vip_id)->select('*')
            ->first() ?: [];

        $storeOptions = Store::where('admin_id', $this->getadminid())->select('*')->get()
            ->toArray();
        return compact('vip', 'storeOptions');
    }


}
